package cookinmaddy.client;

import com.yff.controls.FButton;
import com.yff.controls.FInput;
import com.yff.controls.FList;
import com.yff.controls.FListNode;
import com.yff.controls.FText;
import com.yff.listeners.DomListener;
import com.yff.controls.FDiv;

import cookinmaddy.model.Unit;

import haxe.Serializer;
import haxe.Unserializer;

class UnitView
{
	public static var DOC_ID : String = "";	
	
	var mainDoc : FDiv;
	var innerDoc : FDiv;
	var addDiv : FDiv;
	
	var list : List<Unit>;
	var self : UnitView;
	
	public function new() {self = this;}

	public function show()
	{
		var didiv : FDiv = new FDiv();
		if (mainDoc == null)
		{
			mainDoc = FDiv.getById(DOC_ID);
			var req = "?action=listunits";
			StringTools.htmlEscape(req);
			Ajax.getContentAsync(req, onAjaxReturn_LoadList);
		}
		else
		{
			load();
		}
	}	
	
	private function onAjaxReturn_LoadList(data : Dynamic)
	{
		list = Unserializer.run(data);
		load();
	}
	
	private function load()
	{
		var ulist : FList = new FList();
		mainDoc.clear();
		for (unit in list)
		{			
			var node : FListNode = new FListNode();
			var editbtn : FButton = new FButton("Edit");
			editbtn.addEvent("click", self.onClick_Edit, { node : node, unit : unit } );
			
			var delbtn : FButton = new FButton("Delete");
			editbtn.addEvent("click", self.onClick_Delete, { node : node, unit : unit } );
			
			node.innerHTML = unit.LongName;
			node.appendChild(delbtn);
			node.appendChild(editbtn);
			ulist.appendChild(node);		
		}

		innerDoc = new FDiv();
		innerDoc.id = "innerDoc";
		innerDoc.appendChild(ulist);	
		var btn : FButton  = new FButton("Add");
		btn.addEvent("click", self.onClick_DoAdd, null);
		addDiv = new FDiv();
		addDiv.appendChild(btn);
		innerDoc.appendChild(addDiv);		
		mainDoc.appendChild(innerDoc);	
	}
	
	private function onClick_Edit(input : Dynamic)
	{
		var node : FListNode = input.node;
		var unit : Unit = input.unit;
		node.clear();

		node.appendChild(new FText("Name: "));
		
		var longname_input : FInput = new FInput();
		longname_input.defaultValue = unit.LongName;
		longname_input.id = "longname";
		node.appendChild(longname_input);
		
		node.appendChild(new FText("Symbol: "));
		
		var name_input : FInput = new FInput();
		name_input.defaultValue = unit.Name;
		name_input.id = "name";
		node.appendChild(name_input);
		
		var btn : FButton = new FButton("Update");
		btn.addEvent("click", self.onClick_Update, { node : node, unit : unit } );
		node.appendChild(btn);
		
		var cancelbtn : FButton = new FButton("Cancel");
		cancelbtn.addEvent("click", self.show, null );
		node.appendChild(cancelbtn);
	}

	private function onClick_Delete(args : Dynamic)
	{
		var node : FListNode = args.node;
		var unit : Unit = args.unit;
		var data = Serializer.run(unit);
		var req = "?action=delunit&data=" + data;
		StringTools.htmlEscape(req);
		Ajax.getContentAsync(req, onAjaxReturn_Deleted);		
	}

	private function onAjaxReturn_Deleted(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret != "KO")
		{
			var id : Int = cast data;
			for (unit in list)
			{
				if (unit.ID == id)
				{
					list.remove(unit);
					show();
					break;
				}
			}			
		}
	}
	
	private function onClick_DoAdd()
	{
		addDiv.clear();
		addDiv.appendChild(new FText("Name: "));
		var longname_input : FInput = new FInput() ;
		longname_input.defaultValue = "";
		longname_input.id = "longname";
		addDiv.appendChild(longname_input);
		
		addDiv.appendChild(new FText("Symbol: "));
		var name_input : FInput = new FInput();
		name_input.defaultValue = "";
		name_input.id = "name";
		addDiv.appendChild(name_input);
		
		var btn : FButton = new FButton ("Add");
		btn.addEvent("click", self.onClick_Add, self.addDiv);
		addDiv.appendChild(btn);
		btn = new FButton ("Cancel");
		btn.addEvent("click", self.show, null);
		addDiv.appendChild(btn);
	}
	
	private function onClick_Add(div : FDiv)
	{
		var unit : Unit = new Unit();
		var l : Int = div.childNodes.length;
		for(tmp in div.childNodes)
		{
			var txt : FText = cast tmp ;
			switch(txt.id)
			{
				case "name":
					unit.Name = txt.value;
				case "longname":
					unit.LongName = txt.value;
				default:
			}
		}
		
		var data = Serializer.run(unit);		
		var req = "?action=addunit&data=" + data;
		StringTools.htmlEscape(req);
		Ajax.getContentAsync(req, onAjaxReturn_Added);	
	}	
	
	private function onAjaxReturn_Added(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret != "KO")
		{
			var unit : Unit = Unserializer.run(data);
			list.add(unit);
			show();
		}
		else
			js.Lib.alert(ret);
	}
	
	private function onClick_Update(args : Dynamic)
	{
		var node : FListNode = args.node;
		var unit : Unit = args.unit;
		for(tmp in node.childNodes)
		{
			var txt : FText = cast tmp ;
			switch(txt.id)
			{
				case "name":
					unit.Name = txt.value;
				case "longname":
					unit.LongName = txt.value;
				default:
			}
		}
		
		var data = Serializer.run(unit);
		var req = "?action=updateunit&data=" + StringTools.urlEncode(data);		
		Ajax.getContentAsync(req, onAjaxReturn_Updated);
	}	
	
	private function onAjaxReturn_Updated(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret == "OK")
			show();
		else
			js.Lib.alert(ret);
	}
}